Page({
  data: {
    reviews: [],
    loading: false,
    serviceId: ''
  },

  onLoad(options) {
    this.setData({ serviceId: options.serviceId });
    this.loadReviews();
  },

  loadReviews() {
    this.setData({ loading: true });
    
    wx.cloud.callFunction({
      name: 'getReviews',
      data: {
        serviceId: this.data.serviceId
      },
      success: (res) => {
        this.setData({
          reviews: res.result.data.map(item => ({
            ...item,
            createTime: this.formatTime(item.createTime)
          }))
        });
      },
      fail: () => {
        wx.showToast({
          title: '加载失败',
          icon: 'none'
        });
      },
      complete: () => {
        this.setData({ loading: false });
      }
    });
  },

  formatTime(timestamp) {
    const date = new Date(timestamp);
    return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
  },

  onPullDownRefresh() {
    this.loadReviews(() => {
      wx.stopPullDownRefresh();
    });
  }
});